package com.epam.zhilko.util;

import com.epam.zhilko.entity.Employee;
import org.apache.log4j.Logger;

import javax.persistence.NoResultException;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: admin
 * Date: 11/28/13
 * Time: 3:11 PM
 * To change this template use File | Settings | File Templates.
 */

public class MainServlet extends javax.servlet.http.HttpServlet {
    private static final Logger LOG = Logger.getLogger(MainServlet.class);



    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
            LOG.info("Entered main servlet");
            DAO dao = new DAO();
            int id = Integer.valueOf(request.getParameter("employeeId"));

        try {
            Employee employee = dao.findEmployeeByID(id);
            request.setAttribute("entity", employee);
            getServletContext().setAttribute("entity", employee);
            request.getRequestDispatcher("/WEB-INF/main.jsp").forward(request, response);
            LOG.info("Forwarded back to main jsp");
        } catch (NoResultException e) {
            request.setAttribute("errorMessage", "No employee with id " + id + " found");
        }


    }

}